<template lang="">
  <div>
    <el-tree
      ref="refTree"
      :data="list"
      :props="{ label: 'name' }"
      show-checkbox
      default-expand-all
      :check-strictly="true"
      node-key="id"
    />

    <el-button type="primary" @click="closeFn">取消</el-button>
    <el-button type="success" @click="successFn">确定</el-button>
  </div>
</template>
<script>
import { getPermissionsListAPI } from '@/api/permissions'
import { tranListToTreeData } from '@/utils/tranListToTreeData'
import { getRoleDetailAPI, assignPermAPI } from '@/api/setting'
export default {
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.getPermissionsList()
    this.getRoleDetail()
  },
  methods: {
    closeFn() {
      this.$emit('close')
    },
    successFn() {
      this.assignPerm()
      this.$emit('assSuccess')
    },
    async assignPerm() {
      const permIds = this.$refs.refTree.getCheckedKeys()
      const res = await assignPermAPI({ id: this.id, permIds })
      console.log(res)
    },
    async getPermissionsList() {
      const res = await getPermissionsListAPI()
      const r = tranListToTreeData(res.data)
      console.log(123, r)
      this.list = r
    },
    async getRoleDetail() {
      const res = await getRoleDetailAPI(this.id)
      this.$refs.refTree.setCheckedKeys(res.data.permIds)
      console.log(res.data.permIds)
    }
  }
}
</script>
<style lang="sass"></style>
